/*
 * Copyright 2007 Agile Plugins Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * 	http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.agileplugins.refactor;

import com.intellij.codeInspection.InspectionManager;
import com.intellij.codeInspection.LocalInspectionTool;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.psi.*;

import java.util.ArrayList;
import java.util.List;

import org.jetbrains.annotations.NotNull;

public class RedundantArrayDeclarationInspection extends LocalInspectionTool {
    private Logger logger = Logger.getInstance(getClass().getName());

    public boolean isEnabledByDefault() {
        return true;
    }

    public ProblemDescriptor[] checkField(@NotNull PsiField psiField,
                                          @NotNull InspectionManager inspectionManager, boolean b) {
        ProblemDescriptor problemDescriptor = checkForRedundantArray(psiField, inspectionManager);
        if (problemDescriptor == null) {
            return null;
        }
        return new ProblemDescriptor[]{problemDescriptor};
    }

    public ProblemDescriptor[] checkMethod(@NotNull PsiMethod psiMethod, @NotNull InspectionManager inspectionManager, boolean b) {
        List<ProblemDescriptor> list = new ArrayList<ProblemDescriptor>();
        searchForVariables(psiMethod, list, inspectionManager);
        if (list.size() == 0) {
            return null;
        }
        return list.toArray(new ProblemDescriptor[list.size()]);
    }

    private void searchForVariables(PsiElement element, List<ProblemDescriptor> list, InspectionManager inspectionManager) {
        if (element instanceof PsiLocalVariable) {
            ProblemDescriptor problemDescriptor = checkForRedundantArray((PsiVariable) element, inspectionManager);
            if (problemDescriptor != null) {
                list.add(problemDescriptor);
            }
        } else {
            PsiElement[] children = element.getChildren();
            for (PsiElement child : children) {
                searchForVariables(child, list, inspectionManager);
            }
        }
    }

    private ProblemDescriptor checkForRedundantArray(PsiVariable variable, InspectionManager inspectionManager) {
        PsiExpression initializer = variable.getInitializer();
        if (initializer instanceof PsiNewExpression) {
            PsiNewExpression newExpression = (PsiNewExpression) initializer;
            PsiType type = newExpression.getType();
            PsiType fieldType = variable.getType();
            if (type instanceof PsiArrayType && type.equals(fieldType) && newExpression.getArrayInitializer() != null) {
                PsiElement[] children = newExpression.getChildren();
                PsiElement problemDescriptorElement = null;
                for (int i = 1; i < children.length; i++) {
                    if (children[i] instanceof PsiJavaToken) {
                        PsiJavaToken psiJavaToken = (PsiJavaToken) children[i];
                        if (psiJavaToken.getTokenType().equals(JavaTokenType.LBRACKET)) {
                            problemDescriptorElement = children[i - 1];
                            break;
                        }
                    }
                }
                if (problemDescriptorElement != null) {
                    return inspectionManager.createProblemDescriptor(
                            problemDescriptorElement,
                            "Remove redundant array declaration",
                            new RemoveRedundantArrayDeclarationQuickFix(),
                            ProblemHighlightType.LIKE_UNUSED_SYMBOL);
                }
            }
        }
        return null;
    }

    @NotNull
    public String getGroupDisplayName() {
        return "RefactoringHelpers";
    }

    @NotNull
    public String getDisplayName() {
        return "Redundant Array Declaration";
    }

    @NotNull
    public String getShortName() {
        return "RedundantArrayDeclarationInspection";
    }
}
